package com.example.demo.mvvm.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.demo.mvvm.R;
import com.example.demo.mvvm.ui.base.BaseActivity;
import com.example.demo.mvvm.ui.base.BaseViewVM;

public class WebActivity extends BaseActivity {
    private View mRootView;
    private WebView mWebView;
    private Toolbar toolbar;
    private String mDataJson;
    private String mUrl;
    private String mTitle;
    private String mContent;
    private String mImageUrl;
    private int type;
    private Uri mUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
       /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            MIUISetStatusBarLightMode(getWindow(), true);
            getWindow().setStatusBarColor(Color.WHITE);
        }*/
        initView();

    }



    @SuppressLint("JavascriptInterface")
    @SuppressWarnings({"deprecation"})
    private void initView() {
        String activityUrl = getIntent().getStringExtra("activityUrl");
        mUri = Uri.parse(activityUrl);
        type = getIntent().getIntExtra("type", 0);
        mRootView = findViewById(R.id.root);
        mWebView = (WebView) findViewById(R.id.webview_dynamic);
        toolbar = findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mWebView.canGoBack()){
                    mWebView.goBack();
                    return;
                }
                finish();
            }
        });
        mWebView.getSettings().setJavaScriptEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebView.getSettings().setDomStorageEnabled(true);//DOM Storage
        mWebView.loadUrl(activityUrl);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }

            @Override
            public void onPageCommitVisible(WebView view, String url) {
                super.onPageCommitVisible(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view , url);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                toolbar.setTitle(title);
            }
        });

    }



    public class JsOperation {
        Context context;

        public JsOperation(Context c) {
            context = c;
        }

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        } else {
            if (type == 0) {
                setResult(RESULT_OK);
            }
        }
        return super.onKeyUp(keyCode, event);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    protected boolean enableDatabind() {
        return false;
    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public int getVariableId() {
        return 0;
    }

    @Override
    public BaseViewVM getViewModel() {
        return null;
    }

    @Override
    protected void init() {

    }

}
